package cn.qihang.ssm.controller;

import cn.qihang.ssm.pojo.Permission;
import cn.qihang.ssm.pojo.Product;
import cn.qihang.ssm.service.PermissionService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @Author: qihang
 * @Date: 2022/11/4 15:25
 * @Desc:
 */
@Controller
@RequestMapping("permission")
public class PermissionController {

    @Autowired
    private PermissionService permissionService;


    /**
     * 查询所有资源权限
     * @param page
     * @param size
     * @return
     */
    @RequestMapping("findAll")
    public ModelAndView findAll(
            @RequestParam(value = "page",defaultValue = "1") Integer page ,
            @RequestParam(value = "size" , defaultValue = "5") Integer size
    ){

        PageHelper.startPage(page,size);
        List<Permission> permissionList = this.permissionService.findAll(page,size);
        PageInfo pageInfo = new PageInfo(permissionList);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("pageInfo" , pageInfo);
        modelAndView.setViewName("permission-list");
        return modelAndView;
    }

    /**
     * 添加资源权限
     * @return
     */
    @RequestMapping("save")
    public String save(Permission permission){
        this.permissionService.save(permission);
        return "redirect:findAll.do";
    }


    /**
     * 删除资源权限
     * @param id
     * @return
     */
    @RequestMapping("deletePermission")
    public String deletePermission(@RequestParam("id") Integer id) {
        this.permissionService.deletePermission(id);
        return "redirect:findAll.do";
    }
}
